const router = require('koa-router')()
const mysql = require('../mysql')

router.get('/', async (ctx, next) => {
  await ctx.render('index', {
    title: 'Hello Koa hahahahaha!'
  })
})
router.get('/show', async (ctx, next) => {
  const postData = ctx.request.body;
  console.log('postData：', postData);
  let result = await mysql.query('select * from todo',);

  ctx.body = {
    code: 200,
    data: result,
    message: 'show success'
  };
})

router.post('/add', async (ctx, next) => {
  const postData = ctx.request.body;
  console.log('postData：', postData);

  let result = await mysql.query('INSERT INTO todo set ?',postData.params);

  ctx.body = {
    code: 200,
    data: result[0],
    message: 'add success'
  };
})




router.get('/string', async (ctx, next) => {
  ctx.body = 'koa2 string'
})

router.get('/json', async (ctx, next) => {
  ctx.body = {
    title: 'koa2 json'
  }
})

module.exports = router
